--add the affliction to the list of current afflictions

function Stygian_addAff(affliction)
  SDebug("curing","Adding affliction " .. affliction)
  if table.contains(Stygian_currentAfflictions, affliction) then return end
  SDebug("curing","Shit's already added, this may be an illusion")
  if table.contains(Stygian_curing, affliction) then return end
  SDebug("curing","We're currently curing this shit. maybe an illusion")
  table.insert(Stygian_currentAfflictions, affliction)
  SDebug("curing","Shit got added. below is the currentAfflictions list")
  SDebug("curing",Stygian_currentAfflictions)
end

--and one to remove it when it's cured

function Stygian_remAff(affliction)
  if not table.contains(Stygian_curing, affliction) then return end
    SDebug("curing", string.format("Ok, we have %s so it is valid to remove it",affliction))
  for bal,aff in pairs(Stygian_curing) do
    if aff == affliction then 
      SDebug("curing", string.format("Ok, %s is the balance we've said we're curing %s with", bal, aff))
      if bal == "balance" and Stygian_afflictionList[bal][aff] == "writhe" then Stygian.writhing = false end
      Stygian_curing[bal] = "" end
  end
  for index,aff in pairs(Stygian_currentAfflictions) do
    if aff == affliction then 
      SDebug("curing", string.format("Ok, now we're nuking %s in the current affliction list as well, this is probably bad", aff))
      table.remove(Stygian_currentAfflictions, index) end
  end
end

--function to set balance back to 1 if the cure hasn't gone through

function Stygian_balanceRestore(balance)
  SDebug("curing", string.format("Ok, let's check and restore %s balance", balance))
  if Stygian.balances[balance] == 0.5 then 
    SDebug("curing", "The balance is marked as in use, so we think the cure didn't go through")
    local affliction = Stygian_curing[balance]
    Stygian_curing[balance] = ""
    SDebug("curing", "We've reset this balance as not curing anything")
    Stygian.balances[balance] = 1
    SDebug("curing", "we've set the balance as usable")
    Stygian_addAff(affliction)
    SDebug("curing", "we've now readded the affliction")
  else
    SDebug("curingt", string.format("The balance (%s) isn't marked as in use, so we have it. If we think we're curing something on that balance, we should add it back",balance))
    local affliction = Stygian_curing[balance]
    Stygian_curing[balance] = ""
    SDebug("curing", string.format("We've reset this balance (%s) as not curing anything", balance))
    Stygian_addAff(affliction)
    SDebug("curing", string.format("we've now readded the affliction (%s)", affliction))
  end
end



--function to pop item off of currentAfflictions and into curing

function Stygian_addCuring(balance, affliction)
  for index,aff in ipairs(Stygian_currentAfflictions) do
    if aff == affliction then 
      table.remove(Stygian_currentAfflictions, index)
      Stygian_curing[balance] = affliction
    end
  end
end

--function to check if the cure has more than one balance which can cure it

function Stygian_multiCure(affliction)
  if type(Stygian_curingBals[affliction]) == "string" then
    SDebug("curing",string.format("This thing is only cured by one balance: %s and so is not a multicure", Stygian_curingBals[affliction]))
    return false
  elseif type(Stygian_curingBals[affliction]) == "table" then
    SDebug("curing",string.format("This thing is cured by %s balances and so is a multicure", #Stygian_curingBals[affliction]))
    return true
  end
end

function Stygian_multiCom(balance,affliction)
  if type(Stygian_afflictionList[balance][affliction]) == "string" then
    SDebug("curing",string.format("This only has one line to its cure(%s), not multiCom",Stygian_afflictionList[balance][affliction]))
    return false
  elseif type(Stygian_afflictionList[balance][affliction]) == "table" then
    SDebug("curing",string.format("This thing is cured by %i lines  and so is a multiCom", #Stygian_curingBals[affliction]))
    return true
  end
end
--function to check if a balance is open

function Stygian_haveBalance(balance)

  if Stygian.balances[balance] == 1 then 
    SDebug("curing","we have " .. balance .. " balance")
    return true 
  else 
    SDebug("curing","Shit, we don't have " .. balance .. " balance")
    return false 
  end
end

--function to flag all balances we need to scan

function Stygian_flagBalances()
  SDebug("curing", "Now to reset all flags, then flag the balances we need")
  Stygian_balanceFlags = {
    focus = false,
    purgative = false,
    salve = false,
    herb = false,
    balance = false
  }
  SDebug("curing","Running through the list of current afflictions")
  for _,affliction in ipairs(Stygian_currentAfflictions) do
    SDebug("curing", string.format("Now flagging for %s affliction", affliction))
    if not Stygian_multiCure(affliction) then
      SDebug("curing", string.format("Results came back as not multicure... only one balance will cure this(%s) and so we'll set that flag to true", Stygian_curingBals[affliction]))
      Stygian_balanceFlags[Stygian_curingBals[affliction]] = true
    else 
      SDebug("curing", "It can be cured by multiple balances, we'll have to loop and flag all of them")
      for _,balance in ipairs(Stygian_curingBals[affliction]) do
        SDebug("curing", string.format("We're going to flag (%s) now",balance))
        Stygian_balanceFlags[balance] = true
      end
    end
  end
end


--check for crucialAffs and cure them. Loops through the crucial affs table, curing any it finds. 
--it will do this before it moves on to the general current affs. 
--Mainly because this will come first in the prompt trigger.


function Stygian_cureCrucial()
  SDebug("curing", "Cure our crucial afflictions")
  for _,affliction in ipairs(Stygian_crucialAffs) do
    SDebug("curing", string.format("Crucial: %s is being check", affliction))
    if Stygian_afflictedBy(affliction) then
      SDebug("curing", string.format("Crucial: %s is one we have", affliction))
      if not Stygian_multiCure(affliction) then
        SDebug("curing", string.format("Crucial: It's a one-liner, going to run Stygian_cure(%s, %s)",Stygian_curingBals[affliction], affliction))
        Stygian_cure(Stygian_curingBals[affliction], affliction)
      else
        SDebug("curing", "Crucial: This one has multiple parts")
        for _,balance in ipairs(Stygian_curingBals[affliction]) do
          SDebug("curing", string.format("Crucial: We're running through all balance, currently checking %s", Stygian_curingBals[affliction]))
          if Stygian_haveBalance(balance) then 
            SDebug("curing", string.format("We have the %s balance, so we'll cure using Stygian_cure(%s, %s)",balance, balance, affliction))
            Stygian_cure(balance, affliction)
            break
          end
        end
      end
    end
  end
end

--function to loop through the balances flagged and cure them!
function Stygian_balanceCure()
  SDebug("curing", "Now running through the balances to cure")
  for _,balance in ipairs(Stygian_priorities.meta) do
    SDebug("curing", string.format("now check (%s) balance",balance))
    if Stygian_balanceFlags[balance] then 
      SDebug("curing", string.format("Balance (%s) is needed, we'll cure on it", balance))
      Stygian_cure(balance) 
    else
      SDebug("curing", string.format("Balance (%s) is not needed",balance))
    end
  end
end

--check for hinderence to curing
function Stygian_notHindered(balance)
  for _,aff in ipairs(Stygian_afflictionList[balance].hinderedBy) do 
    if table.contains(Stygian_currentAfflictions, aff) or table.contains(Stygian_curing, aff) then 
      return false
    end
  end  
  return true
end

--function to set balance as used and invoke timer for maintenance on the balance
function Stygian_useBal(balance)
  SDebug("curing", string.format("We're setting %s balance as used", balance))
  Stygian.balances[balance] = 0.5
  tempTimer(0.3, string.format([[Stygian_balanceRestore("%s")]],balance))
end

function Stygian_afflictedBy(affliction)
  return table.contains(Stygian_currentAfflictions, affliction)
end
--the meat and potatos curing script
function Stygian_cure(balance, affliction)
  if affliction == nil then 
    SDebug("curing", string.format("We're working on balance %s. Affliction is not specific",balance))
  else
    SDebug("curing", string.format("We're working on balance %s, Affliction is : %s", balance, affliction))
  end
  assert(balance ~= nil, "you're doing it wrong, you have to tell the cure script what balance to scan")
  if Stygian_haveBalance(balance) and Stygian_notHindered(balance) then 
    SDebug("curing", "We have the balance " .. balance .. " and we are not hindered on it")
    if affliction ~= nil then
      SDebug("curing", string.format("We are going to cure: %s", affliction))
      if not Stygian_multiCom(balance,affliction) then
        SDebug("curing", string.format("Ok, %s is a one-liner cure", affliction))
        send(Stygian_afflictionList[balance][affliction])
        Stygian_addCuring(balance, affliction)
        Stygian_useBal(balance)
      else
        local cure = Stygian_afflictionList[balance][affliction]
        for _,cureAction in ipairs(cure) do
          send(cureAction)
        end
        SDebug("curing", string.format("We are marking the %s balance as being used to cure the %s affliction", balance, affliction))
        Stygian_addCuring(balance,affliction)
        SDebug("curing", string.format("We are now going to mark %s balance as in use", balance))
        Stygian_useBal(balance)
      end
    elseif Stygian.tracking == 1 then
      SDebug("curing", "We are tracking, let's run through the list of things on this balance and cure the first one!")
      for _,affliction in ipairs(Stygian_priorities[balance]) do
        SDebug("curing", string.format("Checking to see if we have %s affliction", affliction))
        if Stygian_afflictedBy(affliction) then
          SDebug("curing", string.format("We're afflicted by %s, let's cure it!", affliction))
          if not Stygian_multiCom(balance,affliction) then
            SDebug("curing", string.format("This is a one liner, going to just send the command %s", Stygian_afflictionList[balance][affliction]))
            if balance == "balance" and Stygian_afflictionList[balance][affliction] == "writhe" and (not Stygian.writhing) then
              Stygian.writhing = true
              Stygian_addCuring(balance,affliction)
              send("writhe")
              return
            end
            send(Stygian_afflictionList[balance][affliction])
            SDebug("curing", string.format("We are marking the %s balance as being used to cure the %s affliction", balance, affliction))
            Stygian_addCuring(balance,affliction)
            SDebug("curing", string.format("We are now going to mark %s balance as in use", balance))
            Stygian_useBal(balance)
          else
            SDebug("curing", "This one's got multiple parts")
            local cure = Stygian_afflictionList[balance][affliction]
            for i,cureAction in ipairs(cure) do
              SDebug("curing", string.format("Sending %s",cureAction))
              send(cureAction)
            end
            Stygian_addCuring(balance,affliction)
            Stygian_useBal(balance)
          end
          break
        end
      end
    end
  end
end

